from tkinter import *

# Canvas and Frame 滑动条模块
class ScrollableFrame(Frame):
    def __init__(self, container, *args, **kwargs):

        super().__init__(container, *args, **kwargs)
        # 手动内嵌 canvas width = 180
        canvas = Canvas(self,width = 165,bg = "white")
        scrollbar = Scrollbar(self, orient="vertical", command=canvas.yview)
        self.scrollable_frame = Frame(canvas,bg = "white")
        self.scrollable_frame.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
        canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
        canvas.configure(yscrollcommand=scrollbar.set)
        canvas.pack(side="left", fill="both", expand=True)
        scrollbar.pack(side="right", fill="y")
        # def _on_mousewheel(event):  # 鼠标滚轮
        #     canvas.bind("<ButtonPress-1>", _on_mousewheel)
        #     canvas.yview_scroll(int(-1*(event.delta/120)), "units")
        # canvas.bind("<MouseWheel>",_on_mousewheel)  # 绑定鼠标滚轮




if __name__ in "__main__":
    root = Tk()
    frame = ScrollableFrame(root)
    frame.pack(fill=BOTH, expand=1)
    for i in range(20):
        Button(frame.scrollable_frame, text="%s 示例按钮"%i).pack()
    root.mainloop()
